﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace PrimaryAlgorithm.MyAtoi
{
    class MainProject
    {
        static void Main(string[] args)
        {
            string str = "-5-";
            Console.WriteLine(MyAtoi(str));
            Console.ReadKey();
        }
        public static int MyAtoi(string s)
        {
            s = s.Trim();
            string str = string.Empty;
            foreach (var item in s)
            {
                if (item==45 || item==43)
                {
                    if (str.StartsWith("-")|| str.StartsWith("+") || str.Length>=3)
                    {
                        break;
                    }
                    str += item;
                    continue;
                }
                if (48<=item && item<=57)
                {
                    str += item;
                }
                else
                {
                    break;
                }
            }
  
            BigInteger num = 0;
            try
            {
              num = BigInteger.Parse(str);

            }
            catch (Exception)
            {
                return 0;
            }

            if (num >= int.MaxValue)
            {
                return int.MaxValue;
            }
            else if (num <= int.MinValue)
            {
                return int.MinValue;
            }
            else
                return (int)num;
        }
    }
}
